<template>
  <a-modal
    title="查看详情"
    :width="860"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleCancel"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="用户ID">
              <a-input placeholder="请输入用户ID" v-decorator="['userId', {}]" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="微信标识">
              <a-input placeholder="请输入用户openid" v-decorator="['openid', {}]"  :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="国家">
              <a-input v-decorator="[ 'country', {}]" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="省份">
              <a-input v-decorator="[ 'province', {}]" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="城市">
              <a-input v-decorator="[ 'city', {}]" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="语言">
              <a-input v-decorator="[ 'language', {}]" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="累计天数">
              <a-input v-decorator="[ 'days', {}]" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="用户积分">
              <a-input v-decorator="[ 'integral', {}]" :readOnly="!!model.id" />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="首次访问">
              <a-date-picker placeholder="首次访问日期" format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'visitDate', {}]" :disabled="true" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="首次授权">
              <a-date-picker placeholder="首次授权日期" format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'firstDate', {}]" :disabled="true" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="信息更新">
              <a-date-picker placeholder="信息更新日期" format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'updateDate', {}]" :disabled="true" :readOnly="!!model.id"/>
            </a-form-item>
          </a-col>
        </a-row>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "LiteUserInfoModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/liteuser/liteUserInfo/add",
          edit: "/liteuser/liteUserInfo/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','nickName','gender','language','city','province','country','avatarUrl','openid','frequency','level','days','wxAccount','phoneNum','age','integral','updateUser','userType'))
		  //时间格式化
          this.form.setFieldsValue({firstDate:this.model.firstDate?moment(this.model.firstDate):null})
          this.form.setFieldsValue({visitDate:this.model.visitDate?moment(this.model.visitDate):null})
          this.form.setFieldsValue({updateDate:this.model.updateDate?moment(this.model.updateDate):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.firstDate = formData.firstDate?formData.firstDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.visitDate = formData.visitDate?formData.visitDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.updateDate = formData.updateDate?formData.updateDate.format('YYYY-MM-DD HH:mm:ss'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>